﻿using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.XtraBars.Docking2010.Views.WindowsUI;
using DevExpress.XtraEditors;
using EduFormManager.Models;
using EduFormManager.Models.Repo;
using EduFormManager.Utilities;
using System.Collections.Generic;

namespace EduFormManager.Utils
{
    public static class TileContainerHelper
    {
        public static TileContainer CreateTileContainer(WindowsUIView view, Tile activationTile, string caption = "", String subCaption = "", IContentContainer parent = null)
        {
            TileContainer container = new TileContainer() { Caption = caption, Subtitle = subCaption, Parent = parent };
            if (activationTile != null)
            {
                (container as IContentContainer).Name = activationTile.Tag.ToString();
                activationTile.ActivationTarget = container;
            }
            container.Properties.IndentBetweenItems = 1;
            container.Properties.IndentBetweenGroups = 16;
            container.Properties.ItemSize = 128;
            container.Properties.ShowGroupText = DefaultBoolean.True;
            container.AppearanceGroupText.Font = new Font("Segoe UI Light", 18);

            //view.ContentContainers.Add(container);
            return container;
        }

        public static void InitFormContainerActions(TileContainer con)
        {
            con.Properties.ItemCheckMode = TileItemCheckMode.Single;
            DelegateAction deleteAction = new DelegateAction(() => true, () =>
            {
                Tile checkedTile = (Tile) con.Items.FirstOrDefault(x => x.Checked.HasValue && x.Checked.Value);
                if (checkedTile == null) return;
                if (checkedTile.Tag == null) return;
                int? formId = TagHelper.GetFormDataId(checkedTile.Tag.ToString());
                if (!formId.HasValue)
                    return;
                var view = (WindowsUIView) con.Manager.View;
                Flyout fly = view.ContentContainers.FindFirst(x => x is Flyout && x.Caption == "Сообщение") as Flyout;
                if (fly != null)
                {
                    DialogResult res = GuiUtility.ShowFlyoutMessage(view, fly, "Подтверждение", "Удалить форму?", FlyoutCommand.Yes, FlyoutCommand.No);
                    if (res == DialogResult.No)
                        return;
                }
                switch (((Page)checkedTile.ActivationTarget).Document.ControlName)
                {
                    case "ArchiveFormData1":
                    case "ArchiveFormData2":
                    case "FormData2":
                    case "FormData1":
                    {
                        try
                        {
                            EduFormData data = RepoHelper.Repo.GetById<EduFormData>(formId.Value);
                            if (data != null)
                            {
                                RepoHelper.Repo.Delete(data);
                                con.Items.Remove(checkedTile);
                            }
                        }
                        catch (Exception ex) { }
                        break;
                    }
                    case "ArchiveFormData3":
                    case "FormData3":
                    {
                        try
                        {
                            MunicipalityFormData data = RepoHelper.Repo.GetById<MunicipalityFormData>(formId.Value);
                            if (data != null)
                            {
                                RepoHelper.Repo.Delete(data);
                                con.Items.Remove(checkedTile);
                            }
                        }
                        catch (Exception ex) { }
                        break;
                    }
                }
            })
            {
                Caption = "Удалить форму",
                Type = ActionType.Context,
                Edge = ActionEdge.Right,
                Behavior = ActionBehavior.HideBarOnClick
            };

            con.Actions.Clear();
            con.Actions.Add(deleteAction);
        }

        public static void InitQuestionaryContainerActions(TileContainer con)
        {
            con.Properties.ItemCheckMode = TileItemCheckMode.Single;
            DelegateAction deleteAction = new DelegateAction(() => true, () =>
            {
                BaseTile checkedTile = con.Items.First(x => x.Checked.HasValue && x.Checked.Value);
                if (checkedTile.Tag == null)
                    return;
                int? questionaryId = TagHelper.GetQuestionaryId(checkedTile.Tag.ToString());
                if (!questionaryId.HasValue)
                    return;
                try
                {
                    Questionary questionary = RepoHelper.Repo.GetById<Questionary>(questionaryId.Value);
                    RepoHelper.Repo.Delete(questionary);
                    con.Items.Remove(checkedTile);
                }
                catch (Exception)
                {
                    //добавить логирование и не только тут
                }
            })
            {
                Caption = "Удалить анкету",
                Type = ActionType.Context,
                Edge = ActionEdge.Right,
                Behavior = ActionBehavior.HideBarOnClick
            };

            con.Actions.Clear();
            con.Actions.Add(deleteAction);

            DelegateAction newAction = new DelegateAction(() => true, () =>
            {
                try
                {
                    Questionary questionary = new Questionary() { Title = "Новая анкета" , Questions = new HashSet<Question>()};
                    RepoHelper.Repo.Save(questionary);

                    Tile newTile = TileHelper.CreateQuestionaryTile(questionary, (WindowsUIView) con.Manager.View, con);
                    con.Items.Add(newTile);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            })
            {
                Caption = "Новая анкета",
                Edge = ActionEdge.Right
            };
            con.Buttons.AddAction(newAction);
        }
    }
}
